<?php

/**
 * @author HAU DAI
 * @package affiliate
 */
//Check if one of products have on-sale subproduct
$temp = explode(',',$productIds);
$haveSubOnSale = false;
foreach($temp as $key=>$value){
    $haveSubOnSale = app()->XService->run('Shop.Product.checkOnSaleSub', array('productId' => $value));
    if($haveSubOnSale == true){
        break;
    }
}
?>
<style>
    .row{width:auto;overflow: hidden}
    .row label{display: inline-block;width:83px;float:none; margin-top: 20px}
    .row input{display: inline-block;width:200px;float:none;margin-top: 20px}
    .buttons{text-align: center}
    .buttons input{width:auto; margin-top: 20px}
    #set-on-sale-form{width:400px;margin:0 auto}
</style>
<h3 style="text-align: center">Set Product On Sale</h3>
<?php echo CHtml::beginForm(app()->createUrl('/Shop/admin/product/setOnSale'), 'post', array('id'=>'set-on-sale-form')) ; ?>
    <?php echo CHtml::hiddenField('productIds', $productIds);?>
    <div class="row">
        <?php echo CHtml::label('On sale start :', array('class' => 'control-label','style'=>'text-align: left')); ?>
            <?php
            $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                'name' => 'on_sale_start',
//                'value' => date("M dd, yy"),
//        'flat'=>true,//remove to hide thess datepicker
                'options' => array(
                    'showAnim' => 'slideDown', //'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop'
                    'changeMonth' => true,
                    'changeYear' => true,
                    'dateFormat' => 'M d, yy',
                    'minDate' => date("M d, y",strtotime("now")), // minimum date
//                    'maxDate' => '2099-12-31', // maximum date
                    'beforeShow' => 'js:function(){
                    var selectedDate=$("#on_sale_end").datepicker("getDate");
                    $(this).datepicker("option","maxDate",selectedDate);
                }'
                ),
                'htmlOptions' => array(
                    'style' => 'width : 200px',
                    'readonly' => 'readonly',
                ),
            ));
            ?>
    </div>
    <div class="row">
        <?php echo CHtml::label('On sale end :', array('class' => 'control-label',)); ?>

            <?php
            $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                'name' => 'on_sale_end',
//                'value' => date("d/m/Y"),
//        'flat'=>true,//remove to hide the datepicker
                'options' => array(
                    'dateFormat' => 'M d, yy',
                    'showAnim' => 'slideDown', //'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop'
                    'changeMonth' => true,
                    'changeYear' => true,
//                    'minDate' => '2000-01-01', // minimum date
//                    'maxDate' => '2099-12-31', // maximum date
                    'beforeShow' => 'js:function(){
                    var selectedDate=$("#on_sale_start").datepicker("getDate");
                    $(this).datepicker("option","minDate",selectedDate);
                }'
                ),
                'htmlOptions' => array(
                    'style' => 'width : 200px',
                    'readonly' => 'readonly',
                ),
            ));
            ?>

    </div>
    <div class="row buttons">
<!--            --><?php //echo CHtml::submitButton('OK', array('class' => 'btn btn-primary setOnSale')); ?>
            <?php echo CHtml::ajaxSubmitButton('OK',CHtml::normalizeUrl(array('/Shop/admin/product/setOnSale','render'=>true)),
                array(
                    'dataType'=>'json',
                    'type'=>'post',
                    'success'=>'function(data) {
                        alert("Setting on-sale has been saved successfully.");
                        parent.$.fancybox.close();
                    }',
                    'beforeSend'=>'function(){
                    }'
                ),array('class'=>'btn btn-primary setOnSale')); ?>

    </div>
<?php echo CHtml::endForm() ; ?>
<script>
    //validate on_sale_start, on_sale_end before submit
    function validateOnSaleDate() {
        var messageError = '';
        var startDate = $("#on_sale_start").datepicker("getDate");
        var endDate = $("#on_sale_end").datepicker("getDate");
        if(startDate == null){
            messageError += 'On sale start should not be empty \n';
        }
//        if(endDate == null){
//            messageError += 'On sale end should not be empty ';
//        }
        return messageError;
    }

    $('.btn.btn-primary.setOnSale').click(function () {
        var messageError = validateOnSaleDate();
        if(messageError != ''){
            alert(messageError);
            return false;
        }else{
            //Show message confirm apply on-sale date range of product for it's subproducts
            <?php if($haveSubOnSale): ?>
            if (confirm('There are some sub-products on sale. The on-sale start/end date will be applied to all of those items. Are you sure ?')) {
            } else {
                return false;
            }
            <?php endif; ?>
        }
    })
    $('#on_sale_start, #on_sale_end').hover(
        function() {
            $(this).css('cursor', 'pointer');
        }, function() {
            $(this).css('cursor','auto');
        }
    );
</script>

